<?php

namespace App\Http\Controllers\PrizePayment;

use App\Http\Controllers\Controller;
use App\Models\Prize\Prize;
use App\Models\Prize\PrizeService;
use App\Models\PrizePayment\PrizePaymentService;
use App\Models\PrizeRecord\PrizeRecordService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class PaymentController extends Controller
{
    /**
     * 订单支付前确认
     * @param Request $request
     * @param PrizePaymentService $prizePaymentService
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function check(Request $request, PrizePaymentService $prizePaymentService)
    {
        $orderId = $request->get('order_id');
        $paymentInfo = $prizePaymentService->find(['order_id' => $orderId]);
        if (!empty($paymentInfo)) {
            $paymentInfo['amount'] = number_format($paymentInfo['price'] * $paymentInfo['number'], 2);
            $paymentInfo['price'] = number_format($paymentInfo['price'], 2);
        }
        return view('web.prizePayment.orderCheck', array('labelNames' => Prize::LABEL_NAMES, 'orderInfo' => $paymentInfo));
    }

    /**
     * 页面轮询支付结果
     *
     * @param Request $request
     * @param PrizePaymentService $prizePaymentService
     *
     * @return string
     */
    public function poll(Request $request, PrizePaymentService $prizePaymentService)
    {
        $orderId = $request->get('order_id');
        $paymentInfo = $prizePaymentService->find(['order_id' => $orderId]);
        if (!empty($paymentInfo)) {
            if ($paymentInfo->isPaid()) {
                return 'paid';
            } elseif ($paymentInfo->isCanceled()) {
                return 'canceled';
            } elseif ($paymentInfo->isFailed()) {
                return 'Failed';
            }
        }
        return 'null';
    }

    /**
     * 撤销支付，重置支付状态为未支付
     * @param Request $request
     * @param PrizePaymentService $prizePaymentService
     * @return \Illuminate\Http\JsonResponse
     */
    public function revoke(Request $request, PrizePaymentService $prizePaymentService)
    {
        $orderId = $request->input('order_id');
        $paymentInfo = $prizePaymentService->find(['order_id' => $orderId]);
        if ($paymentInfo && !$paymentInfo->isPaid()) {
            if ($paymentInfo->revoke()) {
                Log::debug('撤销支付:' . $orderId);
                return $this->respondJson(['result' => 'success']);
            }
            return $this->respondJson(['result' => 'fail', 'code' => 1]);
        }
        return $this->respondJson(['result' => 'revocable', 'code' => 1]);
    }

    /**
     * 删除订单
     * @param Request $request
     * @param PrizeService $prizeService
     * @param PrizePaymentService $prizePaymentService
     * @param PrizeRecordService $prizeRecordService
     * @return \Illuminate\Http\JsonResponse
     */
    public function delete(Request $request, PrizeService $prizeService, PrizePaymentService $prizePaymentService, PrizeRecordService $prizeRecordService)
    {
        $orderId = $request->input('order_id');
        $paymentInfo = $prizePaymentService->find(['order_id' => $orderId]);
        if ($paymentInfo && $paymentInfo->isRemovable()) {
            $removeResult = $paymentInfo->delete();
            if (!empty($paymentInfo->prize)) {
                $removeResult = $paymentInfo->prize->delete();
            }
            $prizeRecord = $prizeRecordService->getByConditions(array('prize_id' => $paymentInfo->prize_id));
            if (!empty($prizeRecord->toArray())) {
                $removeResult = $prizeRecordService->delete(array('prize_id' => $paymentInfo->prize_id));
            }
            if ($removeResult) {
                Log::debug('删除订单:' . $orderId);
                return $this->respondJson(['result' => 'success']);
            } else {
                Log::error($removeResult);
                return $this->respondJson(['result' => 'fail', 'code' => 1]);
            }
        } else {
            return $this->respondJson(['result' => 'unsuppressible', 'code' => 1]);
        }
    }
}
